package com.tuyrt.windy.common

import android.app.Application
import java.lang.reflect.InvocationTargetException

/**
 * Created by tuyrt7 on 2021/4/13.
 */
object AppGlobals {

    private lateinit var sApplication: Application

    @JvmStatic
    fun getApp(): Application {
        if (!::sApplication.isInitialized) {
            try {
                //java reflect
                sApplication = Class.forName("android.app.ActivityThread")
                    .getDeclaredMethod("currentApplication")
                    .invoke(null) as Application

                //kotlin reflect : need implementation 'org.jetbrains.kotlin:kotlin-reflect'
//                sApplication = Class.forName("android.app.ActivityThread").kotlin
//                    .declaredFunctions.first {it.name == "currentApplication"}.call() as Application
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            } catch (e: NoSuchMethodException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            } catch (e: InvocationTargetException) {
                e.printStackTrace()
            }
        }
        return sApplication
    }
}